package com.googlecode.lohr.tools.utilities;

import java.io.File;
import java.util.ArrayList;
import java.util.StringTokenizer;

import com.googlecode.lingwah.ParseContext;
import com.googlecode.lingwah.ParseResults;
import com.googlecode.lingwah.Parser;
import com.googlecode.lohr.tools.language.LohrDOMBuilder;
import com.googlecode.lohr.tools.language.LohrGrammar;

import lohr.lang.dom.LohrElement;

public class LohrUtilities {
	public static final File[] parseClasspath(String classpath) {
		StringTokenizer tokenizer= new StringTokenizer(classpath, ":;");
		ArrayList<File> files= new ArrayList<File>();
		while(tokenizer.hasMoreTokens()) {
			files.add(new File(tokenizer.nextToken()));
		}
		return files.toArray(new File[files.size()]);
	}
	
	public static LohrElement parseUnit(String text) {
			ParseContext context= new ParseContext(text);
			//context.trace(LohrGrammar.DEFINITION.Identifier, false);
			ParseResults results= context.getParseResults(LohrGrammar.DEFINITION.CompilationUnit, 0);
			
			if (!results.success()) {
				assertTrue("Match failed at position "+results.getErrorPosition()+":"+results.getErrorMessage(), results.success());
			
			int length= results.longestLength();
			assertEquals("Failed to match entire text", text.length(), length);
			
			return results;
		}
		protected LohrElement getCST(Parser matcher, String text) {
			ParseResults results= testParser(matcher, text);
			return LohrDOMBuilder.process(results);
		}
	}
}
